package cn.spring.grab.controller.web;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.spring.grab.common.web.Result;
import cn.spring.grab.domain.WebCode;
import cn.spring.grab.service.IWebCodeService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ycs
 * @date 2024/11/1
 */
@Slf4j
@RestController
@RequestMapping("/init/web")
@RequiredArgsConstructor
public class InitWebController {

    private final IWebCodeService webCodeService;
    private static final String WEB_GET_URL = "https://yixiaobin.xyz/api/code/getPageList?pageNum=1&pageSize=10000";
    private static final String WEB_DETAIL_URL = "https://yixiaobin.xyz/api/code/getCodeDetails?codeId=";

    @RequestMapping("/beGinInit")
    public String beGinInit() {
        String response = HttpRequest.get(WEB_GET_URL).execute().body();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Result result = objectMapper.readValue(response, Result.class);
            if (result.isOk()) {
                JSONObject jsonObject = objectMapper.readValue(JSONUtil.toJsonStr(result.getData()), JSONObject.class);
                List<WebCode> list = jsonObject.getBeanList("list", WebCode.class);
                for (WebCode webCode : list) {
                    String body = HttpRequest.get(WEB_DETAIL_URL + webCode.getCodeId()).execute().body();
                    Result itemResult = objectMapper.readValue(body, Result.class);
                    if (itemResult.isOk()) {
                        BeanUtil.copyProperties(itemResult.getData(), webCode);
                        WebCode old = webCodeService.getOne(Wrappers.<WebCode>lambdaQuery().eq(WebCode::getCodeId, webCode.getCodeId()));
                        if (old != null) {
                            webCode.setId(old.getId());
                        }
                    }
                }
                ThreadUtil.execute(() -> webCodeService.saveOrUpdateBatch(list));

            }
        } catch (JsonProcessingException e) {
            log.info("读取数据失败{}, \n原因：{}", e.getMessage(), response);
        }
        return response;
    }

    @RequestMapping("/preview")
    public ResponseEntity<String> preview(String codeId) {
        WebCode webCode = webCodeService.getOne(Wrappers.<WebCode>lambdaQuery().eq(WebCode::getCodeId, codeId));
        if (webCode != null) {
            return ResponseEntity.status(HttpStatus.OK).body(webCode.getCodeContent());
        }
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("error param codeId request");
    }
}
